<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>子组件向父组件传递数据</title>
</head>
<body>
<div id="app">
    <!--在父组件中处理子组件发送的消息-->
    <cpn v-on:btn-clicked="clicked"></cpn>
</div>

<template id="cpn">
    <label for="info">向父组件传递内容</label>
    <input type="text" name="info" id="info" v-model="info">

    <button v-on:click="sendContent">发送</button>
</template>

<script type="module">
    import {createApp, defineComponent, ref} from "../../../js/vue.esm-browser.js";

    let component = defineComponent(
        {
            template: "#cpn",
            emits: ["btn-clicked"],
            setup(props, content) {
                let info = ref(null)

                function sendContent() {
                    // 发送自定义小写，第一个参数为自定义消息名称，第二个参数为消息内容
                    // 消息的传递：由外向内传递
                    // 消息的处理：由内向外处理
                    content.emit("btn-clicked", info.value)
                }

                return {
                    info, sendContent,
                }
            }
        }
    );

    let app = createApp(
        {
            setup() {
                function clicked(event) {
                    console.log(event);

                }

                return {
                    clicked,
                }
            }
        }
    );

    app.component("cpn", component)
    app.mount("#app")
</script>
</body>
</html>